﻿using System;
using System.Net;
using System.IO;

namespace WaterRegimenMonitor.Common.Pinyin
{
    /// <summary>
    /// 汉字字符单元
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2010/05/27 14:58    创建
    /// </history>
    public sealed class CharUnit
    {
        #region ---Public Method

        /// <summary>
        /// 从给定的二进制读取器中读取汉字字符单元
        /// </summary>
        /// <param name="binaryReader">包含汉字字符单元信息的二进制读取器</param>
        /// <returns>从给定的二进制读取器中读取的汉字字符单元</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:19    创建
        /// </history>
        public static CharUnit Load(BinaryReader binaryReader)
        {
            //创建字符单元
            CharUnit unit = new CharUnit();

            //读取字符
            unit.Char = binaryReader.ReadChar();

            //读取笔画数
            unit.StrokeNumber = binaryReader.ReadByte();

            //读取拼音总数
            unit.PinyinCount = binaryReader.ReadByte();

            //创建拼音序号
            unit.PinyinIndexList = new short[unit.PinyinCount];

            //循环读取拼音序号
            for (int i = 0; i < unit.PinyinCount; i++)
            {
                //读取拼音序号
                unit.PinyinIndexList[i] = binaryReader.ReadInt16();
            }

            //返回读取的字符单元
            return unit;
        }

        /// <summary>
        /// 将字符单元保存到给定的二进制写入器重
        /// </summary>
        /// <param name="binaryWriter">用于保存字符单元的二进制写入器.</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:23    创建
        /// </history>
        public void Save(BinaryWriter binaryWriter)
        {
            //写入字符
            binaryWriter.Write(this.Char);

            //写入笔画数
            binaryWriter.Write(this.StrokeNumber);

            //写入拼音数
            binaryWriter.Write(this.PinyinCount);

            //循环写入拼音序号
            for (int i = 0; i < this.PinyinCount; i++)
            {
                //写入拼音序号
                binaryWriter.Write(this.PinyinIndexList[i]);
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 字符
        /// </summary>
        public char Char
        {
            get;
            set;
        }

        /// <summary>
        /// 拼音数目
        /// </summary>
        public byte PinyinCount
        {
            get;
            set;
        }

        /// <summary>
        /// 拼音索引列表
        /// </summary>
        public short[] PinyinIndexList
        {
            get;
            set;
        }

        /// <summary>
        /// 笔画数
        /// </summary>
        public byte StrokeNumber
        {
            get;
            set;
        }

        #endregion
    }
}
